#include "msgsgexamp.h"
#include <stdlib.h>
#include <time.h>  // 补充头文件

int main() {
    int index = 1;
    struct my_msg_st some_data;
    int msgid;
    char buffer[BUFSIZ];  // 使用标准宏 BUFSIZ

    msgid = msgget((key_t)MSG_KEY, IPC_CREAT | S_IRUSR | S_IWUSR); 
    if (msgid == -1) {
        perror("create message queue failed ");
        return -1;
    }

    srand((int)time(0));
    while (index < 5) {
        fgets(buffer, BUFSIZ, stdin);
        some_data.my_msg_type = rand() % 3 + 1; 
        strcpy(some_data.text, buffer);
        // 修正 msgsnd 的第三个参数为消息内容长度
        if (msgsnd(msgid, (void *)&some_data, sizeof(some_data.text), 0) == -1) { 
            perror("msgsnd failed\n");
            exit(-1);
        }
        index++;
    }
    exit(0);
}
